package com.ruoyi.dataserver.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.dataserver.utils.R;
import com.ruoyi.dataserver.vo.PgPontEntity;
import com.ruoyi.dataserver.service.ILxAnalyzeAnalyzeService;
import com.ruoyi.dataserver.service.ISearchTableService;
import com.ruoyi.dataserver.vo.PgLineEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName
 * @Package com.lab.dataserver.controller
 * @ClassName LxControllerAnalyzeController
 * @Description 流向分析控制器
 * @Company lab
 * @Author wangw
 * @Date 2020/7/20 0020 18:03
 * @UpdateUser
 * @UpdateDate
 * @UpdateRemark
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/lxAnalyzeController")
@Slf4j
public class LxAnalyzeController {

    @Autowired
    private ILxAnalyzeAnalyzeService lxAnalyzeAnalyzeService;

    @Autowired
    public ISearchTableService tableService;

    @Autowired
    public JdbcTemplate jdbcTemplate;
    /**
     * @Title  流向分析接口
     * @Description
     * @return
     * @author wangw
     * @date 2020/7/10 0010 10:57
     */
    @RequestMapping(value = "/analyze",method = RequestMethod.POST)
    public R upStreamAnalyze1(@RequestBody Map map){
        if(StrUtil.isBlank(map.get("DM").toString())){
            return R.failed("参数为null.");
        }
        String dm = map.get("DM").toString();
        return R.ok(lxAnalyzeAnalyzeService.lxAnalyze(dm));
    }

    /**
     * 纵断面分析(此管线的所有上游和下游...)
     */
    @RequestMapping(value = "/zdmAnalyse",method = RequestMethod.POST)
    public R zdmAnalyse(@RequestBody PgLineEntity pgLineEntity) {
        // 获取上游3, 自己 ,下游3 这种格式的管线
        List<PgLineEntity> pgLineEntities = lxAnalyzeAnalyzeService.zdmAnalyse(pgLineEntity);
        // 循环填充管点数据, 同时判断管点是否含有埋深, 如果不含有埋深则计算相邻两个管最深的点作为管点的埋深
        for (PgLineEntity lineEntity : pgLineEntities) {
            StringBuffer sb = new StringBuffer();
            String pointtable = tableService.searchPointTable(lineEntity.getDm());
            String sql = sb.append("select * from \"").append(pointtable)
                    .append("\" where wtdh ='").append(lineEntity.getQddh()).append("'")
                    .toString();
            RowMapper<PgPontEntity> rowMapper1  = new BeanPropertyRowMapper<PgPontEntity>(PgPontEntity.class);
            List<PgPontEntity> pointList = jdbcTemplate.query(sql,rowMapper1);
            PgPontEntity qdPoint = pointList.get(0);
            qdPoint.setGdgc(lineEntity.getQdgc());
            if (qdPoint.getJs() == null || NumberUtil.isLessOrEqual(new BigDecimal(qdPoint.getJs()), BigDecimal.valueOf(0.0))) {
                qdPoint.setJs(getMaxPointMs(qdPoint, lineEntity, pgLineEntities).toString());
            }
            lineEntity.setQdPointEntity(qdPoint);

            StringBuffer sb1 = new StringBuffer();
            String sql1 = sb1.append("select * from \"").append(pointtable)
                    .append("\" where wtdh ='").append(lineEntity.getZddh()).append("'")
                    .toString();
            List<PgPontEntity> pointList1 = jdbcTemplate.query(sql1,rowMapper1);
            PgPontEntity zdPoint = pointList1.get(0);
            zdPoint.setGdgc(lineEntity.getZdgc());
            if (zdPoint.getJs() == null || NumberUtil.isLessOrEqual(new BigDecimal(zdPoint.getJs()), BigDecimal.valueOf(0.0))) {
                zdPoint.setJs(getMaxPointMs(zdPoint, lineEntity, pgLineEntities).toString());
            }
            lineEntity.setZDPointEntity(zdPoint);
        }
        return R.ok(pgLineEntities);
    }

    /** 当前管点的埋深如果不存在, 则根据相邻的两根管线, 取最长的埋深 + 管径最为管点的埋深 */
    public Double getMaxPointMs(PgPontEntity point, PgLineEntity currentLine, List<PgLineEntity> pgLineEntities) {
        PgLineEntity afterLine = CollectionUtil.findOne(pgLineEntities, entity -> entity.getQddh().equals(point.getWtdh()) || entity.getZddh().equals(point.getWtdh()));
        Double currentMs = getMs(point, currentLine);
        Double afterMs = getMs(point, afterLine);
        // 管点的埋深应为: 管线的埋深 + 管线的深度
        return NumberUtil.min(currentMs, afterMs);
    }

    /** 判断起点或终点相连, 返回对应埋深 */
    public Double getMs(PgPontEntity currentPoint, PgLineEntity currentLine) {
        if (currentLine.getQddh().equals(currentPoint.getWtdh())) {
            return currentLine.getQdms();
        } else {
            return currentLine.getZdms();
        }
    }


}
